package com.future.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author zlb
 * @date 2022/04/12 19:11
 */
public class WebUtil {
    /**
     * 获取当前请求报文对象
     *
     * @return 当前请求报文对象
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    /**
     * 获取当前返回报文对象
     *
     * @return 当前返回报文对象
     */
    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
    }

    /**
     * 获取请求对象的Cookie
     *
     * @param request    请求对象
     * @param cookieName cookie名
     * @return 对应cookie值
     */
    public static String getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Cookie cookie = getCookie(cookies, cookieName);
            return cookie == null ? null : cookie.getValue();
        }
        return null;
    }

    /**
     * 获取当前请求报文中的cookie值
     *
     * @param cookieName cookie名
     * @return 对应cookie值
     */
    public static String getCookie(String cookieName) {
        HttpServletRequest request = getRequest();
        return getCookie(request, cookieName);
    }

    /**
     * 筛选Cookie数组
     *
     * @param cookies    cookie数组
     * @param cookieName 所需cookie名
     * @return 对应cookie对象
     */
    public static Cookie getCookie(Cookie[] cookies, String cookieName) {
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookieName.equals(cookie.getName())) {
                    return cookie;
                }
            }
        }
        return null;
    }

    /**
     * 获取UserId
     *
     * @return UserId
     */
    public static String getUserId() {
        HttpServletRequest request = getRequest();
        return getCookie(request, "userId");
    }

    /**
     * 设置当前请求的cookie
     * @param cookieName cookie名
     * @param value cookie值
     * @param age cookie生效时间，单位秒（-1：表示当前会话结束后失效，0：表示cookie立即失效，正数：表示在该秒后失效）
     */
    public static void setCookie(String cookieName, String value, int age) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setMaxAge(age);
        cookie.setPath("/");
        getResponse().addCookie(cookie);
    }

    /**
     * 设置当前请求的cookie
     * @param cookieName cookie名
     * @param value cookie值
     * @param age cookie生效时间，单位秒（-1：表示当前会话结束后失效，0：表示cookie立即失效，正数：表示在该秒后失效）
     */
    public static void setCookie(String cookieName, String value, int age, boolean flag) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setMaxAge(age);
        cookie.setPath("/");
        cookie.setHttpOnly(flag);
        getResponse().addCookie(cookie);
    }
}
